/*
 * Copyright (C) 2008 Romain Guy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sd.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.GridView;

import com.sd.activity.R;

public class ShelvesView extends GridView {
    private Bitmap mShelfBackground;
    private Bitmap mShelfBackgroundTop;
    
    private int mShelfWidth;
    private int mShelfHeight;

    public ShelvesView(Context context) {
        super(context);
    }

    public ShelvesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        load(context, attrs, 0);
    }

    public ShelvesView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        load(context, attrs, defStyle);
    }

    private void load(Context context, AttributeSet attrs, int defStyle) {
    	 final Resources resources = getResources();
    	 final Bitmap shelfBackgroundTop = BitmapFactory.decodeResource(resources, R.drawable.shelf_bg_top);
         final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, R.drawable.shelf_bg_main);
         if (shelfBackground != null) {
             mShelfBackground = Bitmap.createScaledBitmap(shelfBackground, context.getResources().getDisplayMetrics().widthPixels, shelfBackground.getHeight(),false);
             if(shelfBackground!=null && !shelfBackground.isRecycled()){
            	 shelfBackground.recycle();
             }
             mShelfBackgroundTop =  Bitmap.createScaledBitmap(shelfBackgroundTop, context.getResources().getDisplayMetrics().widthPixels, shelfBackgroundTop.getHeight(),false);
             if(shelfBackgroundTop!=null && !shelfBackgroundTop.isRecycled()){
            	 shelfBackgroundTop.recycle();
             }
             mShelfWidth = mShelfBackground.getWidth();
             mShelfHeight = mShelfBackground.getHeight();
         }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final int count = getChildCount();
        final int top = count > 0 ? getChildAt(0).getTop() : 0;
        final int shelfHeight = mShelfHeight;
        final int height = getHeight();
        int x0 = 0;
        int y0 = top;
        /*if(top > -mShelfBackgroundTop.getHeight()){
        	canvas.drawBitmap(mShelfBackgroundTop, x0,y0, null);
        	y0 += mShelfBackgroundTop.getHeight();
        }else{
        	canvas.drawBitmap(mShelfBackground, x0,y0, null);
        	y0 += mShelfBackground.getHeight();
        }*/
        
        
        
        for (int y = y0; y < height; y += shelfHeight) {
            canvas.drawBitmap(mShelfBackground, x0, y, null);
        }
        
        super.dispatchDraw(canvas);
    }


}
